//
//  YDBLoginViewController.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2018/8/8.
//  Copyright © 2018年 杨东宝. All rights reserved.
//

import UIKit
class YDBLoginViewController: YDBBaseViewController {
    
    lazy var iconImage:UIImageView = {
        let imageView = UIImageView.init(image: UIImage.init(named: "appicon"))
        view.addSubview(imageView)
        return imageView;
    }()
    
//    lazy var phoneBack:UIView = {
//        let backView = UIView.init();
//        backView.backgroundColor = HexRGB(0xF9FAFC)
//        backView.layer.masksToBounds = true
//        backView.layer.cornerRadius = DIMENSION_VALUE(25)
//        view.addSubview(backView)
//        
//        return backView
//    }()
//    
//    //MARK:-手机号输入框
//    lazy var phoneTextField: UITextField = {
//        let field = UITextField()
//        field.placeholder = "input_phone".localized
//        field.textColor = hexColor(0x666666,0xA7A7B3)
//        field.font = DIMENSION_FONT_OF_SIZE(16)
//        field.keyboardType = .phonePad
//        field.addTarget(self, action: #selector(textValueChanged), for: .editingChanged)
//        phoneBack.addSubview(field)
//        return field
//    }()
//    
    //MARK:-登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("visitor_login".localized, for: .normal)
        btn.setTitleColor(HexRGB(0xffffff), for: .normal)
        btn.titleLabel?.font = DIMENSION_FONT_OF_SIZE(18)
        btn.layer.cornerRadius = DIMENSION_VALUE(25)
        btn.layer.masksToBounds = true
        btn.setBackgroundColor(HexRGB(0xF9FAFC), forState: .disabled)
        btn.setBackgroundColor(HexRGB(0x3D4466), forState: .normal)
        btn.setTitleColor(HexRGB(0x3D4466), for: .disabled)
        btn.isEnabled = false
        btn.addTarget(self, action: #selector(loginrBtn(_ :)), for: .touchUpInside)
        view.addSubview(btn)
        return btn
    }()
    
    //MARK:-协议按钮
    lazy var aggreeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage.init(named:"select" ), for: .selected)
        btn.setImage(UIImage.init(named: "disSelect"), for: .normal)
        btn.addTarget(self, action: #selector(aggreeBtnClick(_ :)), for: .touchUpInside)
        btn.imageView?.contentMode = .scaleAspectFit
        view.addSubview(btn)
        return btn
    }()
    
    
    lazy var agreementLabel: YYLabel = {
        let agreementText = "Privacy_Policy".localized
        var text = NSMutableAttributedString.init(string: agreementText)
        text.lineSpacing = 5;
        text.font = DIMENSION_FONT_OF_SIZE(14)
        text.color = hexColor(0xEEEEEE,0xEEEEEE)
        
        text.setTextHighlight(NSRange(location: 0, length: agreementText.count), color: hexColor(0x3D4466,0x3D4466), backgroundColor: UIColor.clear) { [weak self](containerView, text, range, rect) in
            self?.userPrivacy()
        }
        
        let label = YYLabel()
        label.numberOfLines = 0;
        label.preferredMaxLayoutWidth = kScreenWidth-DIMENSION_VALUE(20);//最大宽度
        label.attributedText = text;
        view.addSubview(label)
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = HexRGB(0xffffff)
        initSubView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // 隐藏首页的导航栏 true 有动画
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // 跳转页面的导航 不隐藏 false
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    func initSubView() {
       
        iconImage.snp.makeConstraints { make in
            make.width.height.equalTo(DIMENSION_VALUE(100))
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(view.snp_top).offset(kTopHeight +  DIMENSION_VALUE(50))
        }
//        phoneBack.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(DIMENSION_VALUE(20))
//            make.right.equalToSuperview().offset(DIMENSION_VALUE(-20))
//            make.height.equalTo(DIMENSION_VALUE(50))
//            make.centerY.equalToSuperview()
//        }
//
//        phoneTextField.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(DIMENSION_VALUE(20))
//            make.right.equalToSuperview().offset(DIMENSION_VALUE(-20))
//            make.height.equalTo(DIMENSION_VALUE(40))
//            make.centerY.equalTo(phoneBack.snp_centerY)
//        }

        loginBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.width.equalTo(kScreenWidth - DIMENSION_VALUE(50))
            make.height.equalTo(DIMENSION_VALUE(50))
            make.bottom.equalTo(view.snp.bottom).offset( -DIMENSION_VALUE(20) - kTabbarSafeBottomMargin)
        }
        
        agreementLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.bottom.equalTo(loginBtn.snp_top).offset( -DIMENSION_VALUE(20))
        }
        
        aggreeBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(agreementLabel.snp_centerY)
            make.width.height.equalTo( DIMENSION_VALUE(20))
            make.right.equalTo(agreementLabel.snp_left).offset(DIMENSION_VALUE(-10))
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
}
extension YDBLoginViewController{
    //MARK:-隐私政策点击事件
    func userPrivacy() {
        // Push
        let webVC = BaseWebViewController.init(with: "https://api.kytingting.com/privacy.html", navigType: .navbar)
        self.navigationController?.pushViewController(webVC, animated: true)
        DebugLog("隐私政策")
    }
    //MARK:-监听输入
//    @objc func textValueChanged() {
//        if !(phoneTextField.text == ""){
//            loginBtn.isEnabled = true
//        }else{
//            loginBtn.isEnabled = false
//        }
//    }
    //MARK:-登录按钮点击事件
    @objc func loginrBtn(_ btn:UIButton) {
//        if phoneTextField.text?.count ?? 0 <= 0 {
//            view.showToastCenter(message: "input_phone".localized)
//            return
//        }
        if !aggreeBtn.isSelected
        {
            view.showToastCenter(message: "agree_prv".localized)
            return
        }
        DebugLog("登录点击")
        userManger.curUserInfo = UserInfo(nickname: "普通用户", phone: "18888888888");
        userManger.saveUserInfor()
        appManger.jumpToMainVC();
    }
    //MARK:-登录按钮点击事件
    @objc func aggreeBtnClick(_ btn:UIButton) {
        DebugLog("同意按钮点击")
        aggreeBtn.isSelected = !aggreeBtn.isSelected;
        loginBtn.isEnabled = aggreeBtn.isSelected;
    }
}
